<?php

use Phalcon\Mvc\Model\Validator\Email as Email;

use Phalcon\Mvc\Model\Validator\Uniqueness as Uniqueness;

class UserAccountTb extends ModelBase
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $email;

    /**
     *
     * @var string
     */
    public $fname;

    /**
     *
     * @var string
     */
    public $mname;

    /**
     *
     * @var string
     */
    public $lname;

    /**
     *
     * @var string
     */
    public $bdate;

    /**
     *
     * @var string
     */
    public $gender;

    /**
     *
     * @var string
     */
    public $username;

    /**
     *
     * @var integer
     */
    public $activated;

    /**
     *
     * @var string
     */
    public $last_login;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     *
     * @var string
     */
    public $date_modified;

    /**
     *
     * @var string
     */
    public $password;

    /**
     *
     * @var string
     */
    public $profile_pic;

    /**
     *
     * @var string
     */
    public $activation_key;

    public $schools;
    public $skills;
    public $work_experience;
    public $user_type;

    public function getProfilePicUrl($user_id)
    {
        $phql = 'SELECT profile_pic from UserAccountTb
                WHERE id = ?0';
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return $data[0]->profile_pic;   
    }
    public function getFullname($user_id)
    {
        $data = UserAccountTb::findFirstById($user_id);
        return $data->fname.' '.$data->lname;
    }
  
    public function validation()
    {

        $this->validate(
            new Email(
                array(
                    'field'    => 'email',
                    'required' => true,
                )
            )
        );
        if ($this->validationHasFailed() == true) {
            return false;
        }

        $filter = new \Phalcon\Filter();
        $this->fname = $filter->sanitize($this->fname,'string');
        $this->mname = $filter->sanitize($this->mname,'string');
        $this->lname = $filter->sanitize($this->lname,'string');
        $this->username = $filter->sanitize($this->username,'string');
        $this->fname = @ucwords($this->fname);
        $this->mname = @ucwords($this->mname);
        $this->lname = @ucwords($this->lname);
        
        
    }
    public function beforeValidationOnCreate()
    {        

         $this->validate(new Uniqueness(
          array(
                "field"   => "email",
                "message" => "The email is already registered please try another"
            )
        ));

        $this->date_created = CURR_DATE;

        if ($this->validationHasFailed() == true)
        {
            return false;
        }
        if(!$this->profile_pic) $this->profile_pic = DEFAULT_PROFILE_PIC;
    }

    public function beforeValidationOnUpdate()
    {
        $this->date_modified = CURR_DATE;
        if(!$this->profile_pic || empty($this->profile_pic)) {
            $this->profile_pic = DEFAULT_PROFILE_PIC_MALE;
        }

    }

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'email' => 'email', 
            'fname' => 'fname', 
            'mname' => 'mname', 
            'lname' => 'lname', 
            'bdate' => 'bdate', 
            'gender' => 'gender', 
            'username' => 'username', 
            'activated' => 'activated', 
            'last_login' => 'last_login', 
            'date_created' => 'date_created', 
            'date_modified' => 'date_modified', 
            'password' => 'password', 
            'profile_pic' => 'profile_pic', 
            'activation_key' => 'activation_key',
            'schools'=>'schools',
            'skills'=>'skills',
            'work_experience'=>'work_experience',
            'user_type' => 'user_type'
        );
    }

    public function search($category,$searchString)
    {
        switch($category)
        {
            case "People":
                $phql = "SELECT a.city,uat.id as user_id,a.long,a.lat,uat.fname,uat.lname,uat.profile_pic,uat.work_experience 
                         FROM UserAccountTb uat
                         LEFT JOIN UserAddressTb a ON a.user_id = uat.id
                         WHERE CONCAT(uat.fname,' ',uat.lname) LIKE ?0";
                $data = $this->modelsManager->executeQuery($phql,array(0=> '%'.$searchString.'%'));
            break;

            case "Company":

            break;

            case "Groups":

            break;
            
            case "Map":
                $phql = "SELECT a.city,uat.id as user_id,a.long,a.lat,uat.fname,uat.lname,uat.profile_pic,uat.work_experience 
                         FROM UserAccountTb uat
                         LEFT JOIN UserAddressTb a ON a.user_id = uat.id
                         WHERE a.city = ?0";
                $data = $this->modelsManager->executeQuery($phql,array(0=>$searchString));
            break; 
        }
        return $data;
    }
    public function getWorkExperience($json,$type)
    {
        $work_experience = json_decode($json);
        $return = '';
        switch($type)
        {
            case "company":

            break;
            case "work_description":
                $return = $work_experience[0]->work_description;
            break;
        }

        return $return;
    }

    
    public function listNames()
    {
        $phql = "SELECT a.city,uat.id,uat.fname,uat.lname,uat.profile_pic FROM UserAccountTb uat
                 LEFT JOIN UserAddressTb a ON a.user_id = uat.id";
        $data = $this->modelsManager->executeQuery($phql);
        return $data;
    }
    
}
